﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ZDT.Task.Job
{
    public class TaskCenter
    {
        /**/
        /// <summary>
        /// 构造函数
        /// </summary>
        public TaskCenter()
        {
            m_scheduleTasks = new List<TaskItem>();
        }
        /**/
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="newTask">新任务</param>
        public void AddTask(TaskItem newTask)
        {
            m_scheduleTasks.Add(newTask);
        }
        /**/
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="delTask">将要删除的任务，你可能需要停止掉该任务</param>
        public void DelTask(TaskItem delTask)
        {
            m_scheduleTasks.Remove(delTask);
        }
        /**/
        /// <summary>
        /// 启动所有的任务
        /// </summary>
        public void StartAllTask()
        {
            foreach (TaskItem task in ScheduleTasks)
            {
                StartTask(task);
            }
        }
        /**/
        /// <summary>
        /// 启动一个任务
        /// </summary>
        /// <param name="task"></param>
        public void StartTask(TaskItem task)
        {
            //标准启动方法
            if (task.Job == null)
            {
                task.Job += new TimerCallback(task.Execute);
            }
            task.Start();
        }
        /**/
        /// <summary>
        /// 终止所有的任务
        /// </summary>
        public void TerminateAllTask()
        {
            foreach (TaskItem task in ScheduleTasks)
            {
                TerminateTask(task);
            }
        }
        /**/
        /// <summary>
        /// 终止一个任务
        /// </summary>
        /// <param name="task"></param>
        public void TerminateTask(TaskItem task)
        {
            task.Stop();
        }

        /**/
        /// <summary>
        /// 获得所有的
        /// </summary>
        List<TaskItem> ScheduleTasks
        {
            get
            {
                return m_scheduleTasks;
            }
        }
        private List<TaskItem> m_scheduleTasks;
    }
}
